<?php
/**
 * Created by PhpStorm.
 * User: g
 * Date: 2018/3/14
 * Time: 11:55
 */

namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Validate;
use app\index\validate\Valid;

class Qdhb extends Common
{
    /**
     * 我的签到汇报
     */
    public function myqdreport(){
        $qddz = $this->request->param('qddz');
        $start = $this->request->param('start');
        $end = $this->request->param('end');
        $qdlx = $this->request->param('qdlx');
        $this->assign('qddz',$qddz);
        $this->assign('start',$start);
        $this->assign('end',$end);
        $this->assign('qdlx',$qdlx);
        $where = [];
        $where['qdr'] = session('employee_id');
        if($qddz){
            $where['qddz'] = ['like','%'.$qddz.'%'];
        }
        if($start&&!$end){
            $where['qdrq'] = ['>=',$start];
        }elseif(!$start&&$end){
            $where['qdrq'] = ['<=',$end];
        }elseif($start&&$end){
            $where['qdrq'] = [['>=',$start],['<=',$end],'and'];
        }
        if($qdlx){
            $where['qdlx'] = $qdlx;
        }
        $cnt = db('yd_qdhb')->where($where)->count();
        $data = db('yd_qdhb')->where($where)->paginate(10,$cnt);
        $page = $data->render();
        $data = $data->items();
        $this->assign([
            'page'=>$page,
            'data'=>$data,
        ]);
        return $this->fetch();
    }
    /**
     * 下属签到汇报
     */
    public function xiashuqdreport(){
        $qddz = $this->request->param('qddz');
        $qdr = $this->request->param('qdr');
        $qdrbm = $this->request->param('qdrbm');
        $start = $this->request->param('start');
        $end = $this->request->param('end');
        $qdlx = $this->request->param('qdlx');
        $this->assign('qddz',$qddz);
        $this->assign('start',$start);
        $this->assign('end',$end);
        $this->assign('qdlx',$qdlx);
        $this->assign('qdr',$qdr);
        $this->assign('qdrbm',$qdrbm);
        $where = [];
        //查询我负责的部门下属的所有人员
        $bmids = db('department t')->join('department_employee t2','t.id=t2.department_id')->where(['t.principal'=>session('emplyee_id'),'t2.index'=>1])->column('t2.employee_id');
        $where['qdr'] = ['in',$bmids];
        if($qddz){
            $where['qddz'] = ['like','%'.$qddz.'%'];
        }
        if($qdrbm){
            $where['qdrbm'] = ['like','%'.$qdrbm.'%'];
        }
        if($qdr){
            $where['qdr_name'] = ['like','%'.$qdr.'%'];
        }
        if($start&&!$end){
            $where['qdrq'] = ['>=',$start];
        }elseif(!$start&&$end){
            $where['qdrq'] = ['<=',$end];
        }elseif($start&&$end){
            $where['qdrq'] = [['>=',$start],['<=',$end],'and'];
        }
        if($qdlx){
            $where['qdlx'] = $qdlx;
        }
        $cnt = db('yd_qdhb')->where($where)->count();
        $data = db('yd_qdhb')->where($where)->paginate(10,$cnt);
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $v['qdbm'] = db('department t')->where('department_employee t2','t2.department_id=t.id')->where(['t2.index'=>1,'t2.employee_id'=>$v['qdr']])->value('t.de_name');
            $data[$k] = $v;
        }
        $this->assign([
            'page'=>$page,
            'data'=>$data,
        ]);
        return $this->fetch();
    }
    /**
     * 所有签到汇报
     */
    public function allqdreport(){
        $qddz = $this->request->param('qddz');
        $qdr = $this->request->param('qdr');
        $qdrbm = $this->request->param('qdrbm');
        $start = $this->request->param('start');
        $end = $this->request->param('end');
        $qdlx = $this->request->param('qdlx');
        $this->assign('qddz',$qddz);
        $this->assign('start',$start);
        $this->assign('end',$end);
        $this->assign('qdlx',$qdlx);
        $this->assign('qdr',$qdr);
        $this->assign('qdrbm',$qdrbm);
        $where = [];
        if($qddz){
            $where['qddz'] = ['like','%'.$qddz.'%'];
        }
        if($qdrbm){
            $where['qdrbm'] = ['like','%'.$qdrbm.'%'];
        }
        if($qdr){
            $where['qdr_name'] = ['like','%'.$qdr.'%'];
        }
        if($start&&!$end){
            $where['qdrq'] = ['>=',$start];
        }elseif(!$start&&$end){
            $where['qdrq'] = ['<=',$end];
        }elseif($start&&$end){
            $where['qdrq'] = [['>=',$start],['<=',$end],'and'];
        }
        if($qdlx){
            $where['qdlx'] = $qdlx;
        }
        $cnt = db('yd_qdhb')->where($where)->count();
        $data = db('yd_qdhb')->where($where)->paginate(10,$cnt);
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $v['qdbm'] = db('department t')->where('department_employee t2','t2.department_id=t.id')->where(['t2.index'=>1,'t2.employee_id'=>$v['qdr']])->value('t.de_name');
            $data[$k] = $v;
        }
        $this->assign([
            'page'=>$page,
            'data'=>$data,
        ]);
        return $this->fetch();
    }

    /**
     * 签到汇报详情
     */
    public function qdreportdetail(){
        $id = $this->request->param('id');
        $data = db('yd_qdhb')->where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }
}